package GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import com.toedter.calendar.JCalendar;

import Database.AddDbFunctions;
import Database.GetDbFunctions;
import Database.Recs.AccountRec;
import Database.Recs.CategoryRec;


public class MainWindow extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3354162411082206679L;
	private JButton buttonAddDims;
	private JMenuItem menuItemExit;
	private JMenu menuFile;
	private JMenu menuHelp;
	private JMenuBar menu;
	private JLabel labelAccount;
	private JLabel labelCategory;
	private JComboBox comboAccount;
	private JLabel labelSum;
	private JTextField textSum;
	private JButton buttonAdd;
	private JButton buttonExit;
	
	private JTree treeCategory;
	private JScrollPane scrollPanelForTree;
	
	private DefaultComboBoxModel dcbmAccount;
	private DefaultComboBoxModel dcbmCategory;
	private DefaultTreeModel dtm = new DefaultTreeModel(null);

	ArrayList<AccountRec> accounts;
	ArrayList<CategoryRec> categories;
	private JCalendar jCalendar0;
	private static final String PREFERRED_LOOK_AND_FEEL = "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel";
	

	public MainWindow() 
	{
		installLnF();
		initComponents();
		initData();
	}
	
	private static void installLnF() {
        try {
                String lnfClassname = PREFERRED_LOOK_AND_FEEL;
                if (lnfClassname == null)
                        lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
                UIManager.setLookAndFeel(lnfClassname);
        } catch (Exception e) {
                System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
                                + " on this platform:" + e.getMessage());
        }
	}
	
	private void initComponents() {
		setTitle("Vydavky");
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setLayout(null);
		add(getLabelAccount());
		add(getLabelCategory());
		add(getLabelSum());
		add(getComboAccount());
		add(getTextSum());
		add(getButtonAddDims());
		add(getButtonExit());
		add(getScrollPanelForTree());
		add(getButtonAdd());
		add(getJCalendar0());
		setJMenuBar(getMenu());
		setSize(546, 460);
	}

	private JCalendar getJCalendar0(){
		if(jCalendar0==null){
			jCalendar0 = new JCalendar(new Date());
//			jCalendar0.setChangingFirstDayOfWeekAllowed(true);
//			jCalendar0.setControlsPosition(1);
//			jCalendar0.setMaxYearAmount(10);
//			jCalendar0.setMinYearAmount(10);
//			jCalendar0.setShowControls(true);
//			jCalendar0.setTooltipDatePattern("MMMM dd, yyyy");
//			jCalendar0.setWeekDayDisplayType(1);
//			jCalendar0.setYear(2010);
			jCalendar0.setBounds(307, 32, 179, 131);
		}
		return jCalendar0;
	}

	public void initData()
	{
		dcbmAccount.removeAllElements();
		
		accounts = GetDbFunctions.getAccounts();
		
		for (int i = 0; i < accounts.size(); i++) {
			if(accounts.get(i).getBankNumber() != null && accounts.get(i).getBankNumber() != 0)
				dcbmAccount.addElement(accounts.get(i).getAccountNumber() + "/" + accounts.get(i).getBankNumber());
			else
				dcbmAccount.addElement(accounts.get(i).getAccountNumber());
		}
		
		Hashtable<String, CategoryRec> categories = GetDbFunctions.getCategoryHash();
		
		DefaultMutableTreeNode root;
		
		root = new DefaultMutableTreeNode("Root");
		
		loadTreeNodes(root, categories,"",1);
		
		dtm = new DefaultTreeModel(root);
		
		treeCategory.setModel(dtm);
		scrollPanelForTree.repaint();
	}
	
	private void loadTreeNodes(DefaultMutableTreeNode parent, Hashtable<String, CategoryRec> nodes, String structBuilder, int position)
	{
		CategoryRec node;
		String key;
		
		do
		{
			key = structBuilder + (structBuilder.isEmpty() ? "" : ".") + (position < 10 ? "0"+position : position);
			node = nodes.get(key);
			
			if(node != null)
			{
				DefaultMutableTreeNode child = new DefaultMutableTreeNode(node.getName());
				loadTreeNodes(child, nodes, key, 1);
				parent.add(child);
				position++;
			}
		}while(node!=null);
	}

	private JButton getButtonExit() {
		if (buttonExit == null) {
			buttonExit = new JButton();
			buttonExit.setText("Exit");
			buttonExit.setBounds(168, 360, 124, 25);
			buttonExit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {onButtonExit(event);}
			});
		}
		return buttonExit;
	}
	
	private JButton getButtonAdd() {
		if (buttonAdd == null) {
			buttonAdd = new JButton();
			buttonAdd.setText("Add");
			buttonAdd.setBounds(27, 320, 266, 25);
			buttonAdd.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {onButtonAdd(event);}
			});
		}
		return buttonAdd;
	}
	
	private JButton getButtonAddDims() {
		if (buttonAddDims == null) {
			buttonAddDims = new JButton();
			buttonAddDims.setText("AddDims");
			buttonAddDims.setBounds(25, 360, 125, 26);
			buttonAddDims.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {onButtonAddDims(event);} 
			});
		}
		return buttonAddDims;
	}
	
	private JTextField getTextSum() {
		if (textSum == null) {
			textSum = new JTextField();
			textSum.setBounds(124, 290, 159, 19);
		}
		return textSum;
	}
	
	private JLabel getLabelSum() {
		if (labelSum == null) {
			labelSum = new JLabel();
			labelSum.setText("Sum");
			labelSum.setBounds(29, 290, 75, 15);
		}
		return labelSum;
	}
	
	private JLabel getLabelCategory() {
		if (labelCategory == null) {
			labelCategory = new JLabel();
			labelCategory.setText("Category");
			labelCategory.setBounds(29, 73, 91, 15);
		}
		return labelCategory;
	}

	private JLabel getLabelAccount() {
		if (labelAccount == null) {
			labelAccount = new JLabel();
			labelAccount.setText("Account");
			labelAccount.setBounds(29, 37, 87, 15);
		}
		return labelAccount;
	}
	
	private JTree getTreeCategory() {
		if (treeCategory == null) {
			treeCategory = new JTree();
			treeCategory.setModel(dtm);
			treeCategory.setDoubleBuffered(false);
			treeCategory.setBorder(null);
			treeCategory.setSelectionRow(0);
		}
		return treeCategory;
	}
	
	private JScrollPane getScrollPanelForTree() {
		if (scrollPanelForTree == null) {
			scrollPanelForTree = new JScrollPane();
			scrollPanelForTree.setBounds(124, 74, 150, 200);
			scrollPanelForTree.setViewportView(getTreeCategory());
		}
		return scrollPanelForTree;
	}
	
	private JComboBox getComboAccount() {
		if (comboAccount == null) {
			comboAccount = new JComboBox();
			dcbmAccount = new DefaultComboBoxModel();
			comboAccount.setModel(dcbmAccount);
			comboAccount.setDoubleBuffered(false);
			comboAccount.setBorder(null);
			comboAccount.setBounds(126, 34, 157, 24);
		}
		return comboAccount;
	}

	

	private JMenuBar getMenu() {
		if (menu == null) {
			menu = new JMenuBar();
			menu.add(getMenuFile());
			menu.add(getMenuHelp());
		}
		return menu;
	}

	private JMenu getMenuHelp() {
		if (menuHelp == null) {
			menuHelp = new JMenu();
			menuHelp.setText("Help");
			menuHelp.setOpaque(false);
		}
		return menuHelp;
	}

	private JMenu getMenuFile() {
		if (menuFile == null) {
			menuFile = new JMenu();
			menuFile.setText("File");
			menuFile.setOpaque(false);
			menuFile.add(getMenuItemExit());
		}
		return menuFile;
	}

	private JMenuItem getMenuItemExit() {
		if (menuItemExit == null) {
			menuItemExit = new JMenuItem();
			menuItemExit.setText("Exit");
		}
		return menuItemExit;
	}

	

	//ActionListeners
	private void onButtonAddDims(ActionEvent event) 
	{
		new AddDims(this).setVisible(true);
	}
	
	private void onButtonAdd(ActionEvent event) 
	{
		if(treeCategory.getSelectionPath()==null)
			return;
		TreePath[] selectedCategories = treeCategory.getSelectionPaths();
		
		AddDbFunctions.addExpenses((String)comboAccount.getSelectedItem(), selectedCategories,jCalendar0.getDate(), textSum.getText(), "", -1);
		
		textSum.setText("");
		treeCategory.setSelectionRow(-1);
	}

	private void onButtonExit(ActionEvent event) {
		System.exit(0);
	}



}
